package com.njpi.stu.attendance.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njpi.stu.attendance.entity.CheckWork;
import com.njpi.stu.attendance.entity.CheckWorkLog;
import com.njpi.stu.attendance.entity.User;
import com.njpi.stu.attendance.service.ICheckWorkLogService;
import com.njpi.stu.attendance.service.ICheckWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 * 考勤记录表 前端控制器
 * </p>
 *
 * @author pxl
 * @since 2019-11-06
 */
@Controller
@RequestMapping("/check-work-log")
public class CheckWorkLogController {

    @Autowired
    private ICheckWorkLogService checkWorkLogService;
    @Autowired
    private ICheckWorkService checkWorkService;

    /**
     * 分页查询签到记录信息
     * @param model
     * @param session
     * @param currentPage
     * @return
     */
    @RequestMapping(value = "/log/{currentPage}",method = RequestMethod.GET)
    public  String selectOne(Model model,HttpSession session,@PathVariable("currentPage") String currentPage){
        int current=Integer.valueOf(currentPage);
        model.addAttribute("title","签到记录");
        User user= (User) session.getAttribute("user");
        //根据角色分别做不同的处理
        Page page=new Page(current,3);
        QueryWrapper queryWrapper=new QueryWrapper();
        if(user.getType()==0){
            queryWrapper.eq("user_name",user.getRealName());
        }else if (user.getType()==1){
            queryWrapper.eq("operator",user.getRealName());
        }
        IPage iPage = checkWorkLogService.page(page, queryWrapper);
        //计算出最大页数
        int maxPage= (int) (iPage.getTotal()/iPage.getSize());
        if((int)(iPage.getTotal()%iPage.getSize())>0){
            maxPage=maxPage+1;
        }
        model.addAttribute("maxPage",maxPage);
        model.addAttribute("result",iPage);
        return "checkWorkLog";
    }

    /**
     * 签到验证码验证
     * @param verCode
     * @param checkWorkLog
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{verCode}",method = RequestMethod.POST)
    public String saveCheckLog(@PathVariable("verCode") String verCode, @RequestBody CheckWorkLog checkWorkLog, HttpSession session){
        User user = (User) session.getAttribute("user");
        //查询签到的验证码
        CheckWork checkWork = checkWorkService.getById(checkWorkLog.getCheckId());
        String dbVercode = checkWork.getVerCode();
        if(dbVercode.equals(verCode)){
            checkWorkLog.setUserId(user.getUserId());
            checkWorkLog.setUserName(user.getRealName());
            checkWorkLog.setCourseName(checkWork.getCourseName());
            checkWorkLog.setOperator(checkWork.getUserName());
            checkWorkLog.setCreateTime(new Date());
            checkWorkLog.setType(1);
            checkWorkLogService.save(checkWorkLog);
            return "签到成功";
        }else {
            return "验证码输入错误";
        }

    }

    /**
     * 老师修改考勤的信息
     * @param stype
     * @param sid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/log/{id}/{type}",method = RequestMethod.PUT)
    public String updateCheckLog(@PathVariable("type") String stype,@PathVariable("id") String sid){
        Integer id = Integer.valueOf(sid);
        Integer type=Integer.valueOf(stype);
        CheckWorkLog checkWorkLog=new CheckWorkLog();
        checkWorkLog.setId(id);
        checkWorkLog.setType(type);
        checkWorkLogService.updateById(checkWorkLog);
        return "修改成功";
    }
}
